package com.linzm.leetcode.primary.exercises4_20230129;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author zimingl
 * @Date 2023/2/9 22:49
 * @Description: 输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。
 */
public class Demo14 {
    public static void main(String[] args) {
        Demo14 demo14 = new Demo14();
    }

    public int[] reversePrint(ListNode head) {
        if (head == null) {
            return new int[0];
        }
        List<Integer> list = new ArrayList<>();
        list.add(head.val);
        if (head.next == null) {
            return new int[] {head.val};
        }
        ListNode tmp = head.next;
        list.add(tmp.val);
        while (tmp.next != null) {
            tmp = tmp.next;
            list.add(tmp.val);
        }
        Collections.reverse(list);
        return list.stream().mapToInt(num -> num).toArray();
    }

    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
